﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mimoza.Common
{
    public class FieldsFile
    {
        const int cFieldSize = 256;

        public static void WriteField(string value, System.IO.Stream writer)
        {
            if (value == null) value = "";
            byte[] buff = new byte[cFieldSize];
            byte[] textBuff = Encoding.UTF8.GetBytes(value);
            Buffer.BlockCopy(textBuff, 0, buff, 0,
                (textBuff.Length < buff.Length) ? textBuff.Length : buff.Length);
            writer.Write(buff, 0, buff.Length);
        }

        public static string ReadField(System.IO.Stream reader)
        {
            byte[] buff = new byte[cFieldSize];
            reader.Read(buff, 0, buff.Length);
            int endPos = Array.IndexOf(buff, (byte)0);
            if (endPos < 0) endPos = buff.Length;
            return Encoding.UTF8.GetString(buff, 0, endPos);
        }

    }
}
